package com.softeem.oss_music.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.oss_music.comm.R;
import com.softeem.oss_music.entity.MusicList;
import com.softeem.oss_music.entity.MusicListDetails;
import com.softeem.oss_music.service.MusicListDetailsService;
import com.softeem.oss_music.service.MusicListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name = "歌单管理")
@RestController
public class MusicListController {
    @Autowired
    private MusicListService musicListService;
    
    @Autowired
    private MusicListDetailsService musicListDetailsService;
    
    @Operation(summary = "歌单列表", description = "根据提供的页码和数据链条实现分页查询")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "数据条数")
    })
    @GetMapping("/musicList/list")
    public R list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        // 创建分页对象
        IPage<MusicList> iPage = new Page<>(page, limit);
        // 调用page方法进行分页查询
        iPage = musicListService.page(iPage);
        // 从分页对象中获取数据和总数
        List<MusicList> list = iPage.getRecords();
        long count = iPage.getTotal();
        return R.success().setData(list).setCount(count);
    }
    
    @Operation(summary = "歌单详情", description = "根据歌单ID查询歌单详情")
    @Parameters({
            @Parameter(name = "id", description = "歌单ID")
    })
    @GetMapping("/musicList/details")
    public R details(Integer id){
        if (id == null) {
            return R.error().setMessage("歌单ID不能为空");
        }
        MusicList musicList = musicListService.getById(id);
        if (musicList == null) {
            return R.error().setMessage("歌单不存在");
        }
        return R.success().setData(musicList);
    }
    
    @Operation(summary = "添加歌单", description = "添加新歌单")
    @PostMapping("/musicList/add")
    public R add(@RequestBody MusicList musicList){
        if (musicList == null) {
            return R.error().setMessage("歌单信息不能为空");
        }
        if (musicList.getName() == null || musicList.getName().trim().isEmpty()) {
            return R.error().setMessage("歌单名称不能为空");
        }
        // 设置默认值
        if (musicList.getCreateTime() == null) {
            musicList.setCreateTime(new Date());
        }
        if (musicList.getStatus() == null) {
            musicList.setStatus(1); // 默认上架
        }
        boolean result = musicListService.save(musicList);
        return result ? R.success().setMessage("添加成功").setData(musicList) : R.error().setMessage("添加失败");
    }
    
    @Operation(summary = "更新歌单", description = "更新歌单信息")
    @PostMapping("/musicList/update")
    public R update(@RequestBody MusicList musicList){
        if (musicList == null || musicList.getId() == null) {
            return R.error().setMessage("歌单ID不能为空");
        }
        if (musicList.getName() == null || musicList.getName().trim().isEmpty()) {
            return R.error().setMessage("歌单名称不能为空");
        }
        MusicList existingList = musicListService.getById(musicList.getId());
        if (existingList == null) {
            return R.error().setMessage("歌单不存在");
        }
        // 保留创建时间
        musicList.setCreateTime(existingList.getCreateTime());
        boolean result = musicListService.updateById(musicList);
        return result ? R.success().setMessage("更新成功").setData(musicList) : R.error().setMessage("更新失败");
    }
    
    @Operation(summary = "删除歌单", description = "根据歌单ID删除歌单")
    @Parameters({
            @Parameter(name = "id", description = "歌单ID")
    })
    @DeleteMapping("/musicList/delete")
    public R delete(Integer id){
        if (id == null) {
            return R.error().setMessage("歌单ID不能为空");
        }
        // 检查歌单是否存在
        MusicList musicList = musicListService.getById(id);
        if (musicList == null) {
            return R.error().setMessage("歌单不存在");
        }
        // 检查歌单中是否有关联的歌曲
        QueryWrapper<MusicListDetails> query = Wrappers.query(MusicListDetails.class);
        query.eq("list_id", id);
        List<MusicListDetails> detailsList = musicListDetailsService.list(query);
        if (detailsList.size() > 0) {
            return R.error().setMessage("删除失败，请先清空歌单中的歌曲");
        }
        boolean result = musicListService.removeById(id);
        return result ? R.success().setMessage("删除成功") : R.error().setMessage("删除失败");
    }
}
